/**
 * 卡片管理相关函数
 * */
function resetForm() {
	$('form input:text').val('');
	$('form select').each(function(){
		$(this).get(0).selectedIndex = 0;
	});
}

function clearDialog(dialog) {
	$(".serialNumber", dialog).val("");
	$(".cardNumber", dialog).val("");
	$(".ui-widget", dialog).hide();
	dialog[0].card = null;
}

function onViewCard() {
	var s = $("#logTable").getSelected();
	if (s == null || s.length == 0)
		return;
	if (s.length != 1){
		alert("您选择了" + s.length + "条记录, 请选择一条记录查看");
		return;
	}
	var card = s[0];
	$("#viewCardDialog ul:first-child").html("");
	CardAjax.getCardDetail(card, function(data){
		$("#viewCardDialog ul:first-child").html(data);
	});
	$("#viewCardDialog").dialog({
		autoOpen: true,
		height: 300,
		width: 400,
		modal: true,
		title: "查看",
		buttons: {
			'关闭': function() {
				$(this).dialog('close');
			}
		}
	});
}

function onReadCard() {
	var dialog = $("#readCardDialog");
	$("ul", dialog).html("");
	dialog.dialog({
		autoOpen: true,
		height: 300,
		width: 520,
		modal: true,
		title: "读取卡片信息",
		buttons: {
			'刷卡':function(){
				var d = Card.readDetail();
				if (d == null) {
					alert("请确认读卡器已经插上, 并把卡放到读卡器上");
					return;
				}
				$("ul", dialog).html("");
				var html = "";
				html += "<li><label>序列号</label>&nbsp;" + d[0] + "</li>";
				html += "<li><label>锁号</label>&nbsp;" + d[1] + "</li>";
				html += "<li><label>有效期</label>&nbsp;" + d[2] + "</li>";
//				html += "<li><label>卡号</label>&nbsp;" + d[3] + "</li>";
//				html += "<li><label>子卡号</label>&nbsp;" + d[4] + "</li>";
//				html += "<li><label>子号2</label>&nbsp;" + d[5] + "</li>";
				$("ul.left", dialog).html(html);
				CardAjax.getCardDetailBySerialNumber(d[0], function(data){
					$("ul.right", dialog).html(data);
				});
			},
			'关闭': function() {
				$(this).dialog('close');
			}
		}
	});
}

function writeCardNumber(serial, cardNumber, dialog) {
	if (serial == null || cardNumber == null || cardNumber == '') {
		alert(serial);
		alert(cardNumber);
		return;
	}
//	var cn = parseInt(cardNumber);
//	if (cn == NaN || cn < 0 || cn > 65535) {
//		show("卡号输入非法, 卡号范围为: 0-65535", dialog);
//		return;
//	}
	CardAjax.saveCardNumber(serial, cardNumber, function(data){
		if (data == null){
			data = "卡号保存成功";
		}
		show(data, dialog);
	});
}

function onAddCard() {
	var dialog = $("#cardDialog");
	clearDialog(dialog);
	$(".swingCard", dialog).unbind("click").click(function(){
		var cardSerial = Card.readSerial();
		if (cardSerial == null) {
			alert("请放置卡片在读卡器上");
			return;
		}
		dialog[0].card = cardSerial;
		$(".serialNumber", dialog).val(cardSerial);
		CardAjax.addCard(cardSerial, function(error) {
			if (error == null) {
				error = "保存成功";
			}
			$(".result", dialog).text(error);
			$(".ui-widget", dialog).show(100);
		});
	});
//	$(".save", dialog).unbind("click").click(function(){
//	});
	dialog.dialog({
		autoOpen: true,
		height: 240,
		width: 400,
		modal: true,
		title: "录新卡",
		buttons: {
			'下一张': function() {
				clearDialog(dialog);
			},
			'保存卡号': function() {
				var serial = dialog[0].card;
				if (serial == null){
					return;
				}
				var cardNumber = $(".cardNumber", dialog).val();
				writeCardNumber(serial, cardNumber, dialog);
			},
			'关闭': function() {
				dialog.dialog('close');
			}
		}
	});
}

function onEditCard() {
	var dialog = $("#editCardDialog");
	$("ul.left", dialog).html("");
	$("ul.right", dialog).html("");
	$("ul.edit", dialog).css("display", "none");
	hide(dialog);
	dialog[0].card = null;
	dialog.dialog({
		autoOpen: true,
		height: 315,
		width: 650,
		modal: true,
		title: "修改卡信息",
		buttons: {
			'刷卡': function() {
				$(".ui-widget", dialog).hide();
				var d = Card.readDetail();
				if (d == null) {
					show("请把卡放在读卡器上", dialog);
					return;
				}
				dialog[0].card = null;
				$("ul.left", dialog).html("");
				$("ul.right", dialog).html("");
				var html = "";
				html += "<li><label>序列号</label>&nbsp;" + d[0] + "</li>";
				html += "<li><label>锁号</label>&nbsp;" + d[1] + "</li>";
				html += "<li><label>有效期</label>&nbsp;" + d[2] + "</li>";
//				html += "<li><label>卡号</label>&nbsp;" + d[3] + "</li>";
//				html += "<li><label>子卡号</label>&nbsp;" + d[4] + "</li>";
//				html += "<li><label>子号2</label>&nbsp;" + d[5] + "</li>";
				$("ul.left", dialog).html(html);
				CardAjax.getCardDetailBySerialNumber(d[0], function(data){
					$("ul.right", dialog).html(data);
					dialog[0].card = d[0];
					$("ul.edit", dialog).css("display", "");
				});
			},
			'保存卡号': function() {
				var serial = dialog[0].card;
				if (serial == null)
					return;
				var cardNumber = $(".cardNumber", dialog).val();
				writeCardNumber(serial, cardNumber, dialog);
			},
			'关闭': function() {
				$(this).dialog('close');
			}
		}
	});
}

function editCard(card, dialog) {
	$(".ui-widget", dialog).hide();
	$("#serialNumber2", dialog).text(card.serialNumber);
	$("#cardNumber2", dialog).val(t(card.cardNumber));
	$("#cardType2", dialog).text(card.type2);
//	$("#status2", dialog).val(t(card.enable));
	$("#lockNumber2", dialog).text(card.lockNumber);
	$("#roomName2", dialog).text(card.roomName);
	$("#expireTime2", dialog).text(card.expireTime3);
	dialog[0].card = card;
}

function onRecycleCard() {
	var dialog = $("#ecycleCardDialog");
	$("ul.left", dialog).html("");
	$("ul.right", dialog).html("");
	$("ul.edit", dialog).css("display", "none");
	hide(dialog);
	dialog[0].card = null;
	dialog.dialog({
		autoOpen: true,
		height: 315,
		width: 600,
		modal: true,
		title: "回收卡",
		buttons: {
			'刷卡': function() {
				$(".ui-widget", dialog).hide();
				var d = Card.readDetail();
				if (d == null) {
					show("请把卡放在读卡器上", dialog);
					return;
				}
				dialog[0].card = d[0];
				$("ul.left", dialog).html("");
				$("ul.right", dialog).html("");
				var html = "";
				html += "<li><label>序列号</label>&nbsp;" + d[0] + "</li>";
				html += "<li><label>锁号</label>&nbsp;" + d[1] + "</li>";
				html += "<li><label>有效期</label>&nbsp;" + d[2] + "</li>";
//				html += "<li><label>卡号</label>&nbsp;" + d[3] + "</li>";
//				html += "<li><label>子卡号</label>&nbsp;" + d[4] + "</li>";
				$("ul.left", dialog).html(html);
				CardAjax.getCardDetailBySerialNumber(d[0], function(data){
					$("ul.right", dialog).html(data);
					$("ul.edit", dialog).css("display", "");
				});
			},
			'销卡': function() {
				var serial = dialog[0].card;
				if (serial == null)
					return;
				if (confirm("您确认要收回此卡的开房授权?")){
					if (Card.write(serial, '0','0000000000')) {
						CardAjax.cancelCard(serial, function(data){
							if (data) {
								alert("销卡成功");
							} else {
								alert("销卡成功, 但系统中没有相应的卡记录");
							}
						});
					} else {
						alert("销卡失败, 请重试");
					}
				}
			},
			'回收': function() {
				var serial = dialog[0].card;
				if (serial == null)
					return;
				if (confirm("您确认要收回此卡?")){
					if (Card.write(serial, '0','0000000000')) {
						CardAjax.recycleCard(serial, function(data){
							if (data) {
								alert("回收卡成功, 此卡已经不属于任何会员/员工, 如果已经授权为房卡, 也被收回");
							} else {
								alert("销卡成功, 但系统中没有相应的卡记录");
							}
						});
					} else {
						alert("销卡失败, 请重试");
					}
				}
			},
			'关闭': function() {
				$(this).dialog('close');
			}
		}
	});
}

function show(text, context) {
	$(".result", context).text(text);
	$(".ui-widget", context).show();
}
//
function hide(context){
	$(".result", context).text("");
	$(".ui-widget", context).hide();
}

function t(v) {
	if (v === null)
		v = "";
	return "" + v;
}

function onSelectAll() {
	$("#logTable").selectAll();
}

function onDeselectAll() {
	$("#logTable").deselectAll();
}

function onEnableCards(enable) {
	enable = !!enable;
	var s = $("#logTable").getSelected();
	if (s == null || s.length <= 0)
		return;
	var msg = "你确认要" + (enable?"启用":"禁用") + "这 " + s.length + " 张卡?";
	if (!window.confirm(msg))
		return;
	CardAjax.enableCards(enable, s, function(data){
		if (data != null) {
			alert(data);
		} else {
			$("#logTable").refreshGrid();
		}
	});
}

function test(){
	var s = $("#logTable").getSelected();
}

$(document).ready(function() {
	Calendar.setup({
	    inputField     :    "expireTime",
	    ifFormat       :    "%Y-%m-%d %H:%M",
	    showsTime      :    true,
	    button         :    "btn_expire",
	    singleClick    :    false,
	    step           :    1
	});
	
	$("#swingCard").click(function(){
		var card = readCardSerial();
		if (card != null)
			$("input:text", $(this).parent()).val(card);
	});

	var tableHeight = $(document).height()- $("#tabs2").offset().top - 74 - 27;	//253
	$("#logTable").flexigrid({
		url: window.cardUrl,
		dataType: 'json',
		colModel : [
			{display: '卡号', name : 'cardNumber', width : 170, sortable : false, align: 'center'},
			{display: '类型', name : 'type', width : 100, sortable : false, align: 'center'},
			{display: '状态', name : 'enable', width : 100, sortable : false, align: 'center'},
			{display: '持用者', name : 'holder', width : 100, sortable : false, align: 'left'},
			{display: '序列号', name : 'serialNumber', width : 180, sortable : false, align: 'left'},
			{display: '有效期', name : 'expire', width : 167, sortable : false, align: 'left'}
			],
		buttons : [
					{name: '全选', icon: 'i17.gif', onpress : onSelectAll},
					{name: '不选', icon: 'i18.gif', onpress : onDeselectAll},
					{separator: true},
					{name: '查看', icon: 'i16.gif', onpress : onViewCard},
					{name: '读卡', icon: 'cart_put.png', onpress : onReadCard},
					{separator: true},
					{name: '新卡', icon: 'add.gif', onpress : onAddCard},
					{name: '修改', icon: 'shield_go.png', onpress : onEditCard},
					{name: '回收', icon: 'i11.gif', onpress : onRecycleCard},
					{separator: true},
					{name: '启用', icon: 'hmenu-unlock.png', onpress : function(){onEnableCards(true);}},
					{name: '禁用', icon: 'hmenu-lock.png', onpress : function(){onEnableCards(false);}},
					{separator: true}
					],
		baseIconPath:window.contextPath+'/images/small/',
//		searchitems : [],
//		sortname: "time",
//		sortorder: "desc",
		usepager: true,
//		title: '系统日志',
		useRp: false,
		rp: 15,
		showTableToggleBtn: false,
		showToggleBtn: false,
		minheight: 30,
		resizable:false,
		pagestat: '第 {from} - {to} 条记录, 共 {total} 条',
		pagetext: '第',
		outof: '页, 共',
		totalpage: ' 页',
		nomsg: '没有找到相关记录',
		procmsg: '正在请求, 请稍等; 如果长时间没有反应, 请刷新',
		errormsg: '连接错误',
//		width: 700,
		height: tableHeight
	});
} );
